<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>编辑</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="/node_modules/LobsterUIFrame/weui/css/weui.css" />
    <link rel="stylesheet" href="/node_modules/LobsterUIFrame/weui/css/weuix.css" />
    <script src="/node_modules/LobsterUIFrame/weui/js/zepto.min.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/zepto.weui.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/swipe.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/macy.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/php.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/picker.city.js"></script>
    <script src="/lobstersdk/lobster.js"></script>
    <script src="/jslib/template-web.js"></script>
    <style>

    </style>
</head>

<body ontouchstart>

    <form id="addressform">
        <div class="weui-cells weui-cells_form" id="user">
            <div class="weui-cell">
                <div class="weui-cell__hd">
                    <label class="weui-label">收货人</label>
                </div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="text" placeholder="请填写收货人姓名" name="Addressee">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" name="Tel" type="text" placeholder="请填写收货人手机号">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">所在地区</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" name="Address" id="address" type="text" value="">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <textarea name="Street" class="weui-textarea" placeholder="街道、楼牌号等" rows="3"></textarea>
                </div>
                <i class="weui-icon-clear" onclick="cleararea(this)"></i>
            </div>
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">设为默认</div>
                <div class="weui-cell__ft">
                    <input name="IsDefault" class="weui-switch" type="checkbox">
                </div>
            </div>
            <div class="weui-btn-area">
                <a href="javascript:void(0)" id="save" class="weui-btn weui-btn_primary">保存</a>
            </div>
        </div>
    </form>
</body>

</html>

<script>
    function cleararea(obj) {
        $(obj).prev().find('.weui-textarea').val("").next().find("span").text(0);
        return false;
    }
    function formateDate(fmt, date) {
        var ret;
        var opt = {
            "y+": date.getFullYear().toString(),
            "M+": (date.getMonth() + 1).toString(),
            "d+": date.getDate().toString(),
            "H+": date.getHours().toString(),
            "m+": date.getMinutes().toString(),
            "s+": date.getSeconds().toString()
        };
        for (var k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    }
    lobsterh5.main({
        data: {
            address: {
                AddressID: 0,
                Addressee: "",
                Address: "",
                Tel: "",
                ProvinceCode: "",
                Province: "",
                CityCode: "",
                City: "",
                DistrictCode: "",
                District: "",
                Street: "",
                Sort: 0,
                IsDefault: 0,
                IsDel: 0,
                CreateTime: formateDate(("yyyy-MM-dd HH:mm:ss"), new Date()),
                CreateUser: "",
                CreateUserID: localStorage.getItem("userId")
            },
        },

        //初始化页面
        pageload: function () {
            this.initevent();
            this.getaddressinfo();
        },
        getaddressinfo: function () {
            var self = this;
            var addressId = lobsterh5.GetUrlParam("addressId");
            if (addressId)
                lobsterh5.GET("/sso/v1/patient/GetAddressDetail", { id: addressId }).then(res => {
                    var addressInfo = res.addressDetail;
                    addressInfo.Address = addressInfo.Province + " " + addressInfo.City + " " + addressInfo.District;
                    lobsterh5.WeUISetForm('#addressform', addressInfo);
                    $("[name='Address']").val(addressInfo.Address);
                    $("[name='Street']").val(addressInfo.Street);
                    if (addressInfo.IsDefault == 1)
                        $("[name='IsDefault']").attr("checked", true);
                    self.data.address = addressInfo;

                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                });
        },
        //初始化事件
        initevent: function () {
            var self = this;
            //保存数据
            $("#save").click(function () {
                var formval = lobsterh5.WeUIGetForm('#addressform', self.data.address);
                var on = $("[name='IsDefault']").is(':checked');
                if (on) {
                    self.data.address.IsDefault = 1;
                } else {
                    self.data.address.IsDefault = 0;
                }
                var address = self.data.address;
                address.Street = $("[name='Street']").val();
                $.showLoading("保存中...");
                if (!address.Addressee) {
                    $.toptip("收货人姓名不能为空");
                    $.hideLoading();
                    return;
                }
                if (!address.Tel) {
                    $.toptip("号码不能为空");
                    $.hideLoading();
                    return;
                }
                if (!(/^1[23456789]\d{9}$/.test(address.Tel))) {
                    $.toptip("号码格式错误");
                    $.hideLoading();
                    return;
                }
                if (!address.Address) {
                    $.toptip("请选择所在地区");
                    $.hideLoading();
                    return;
                }
                if (!address.Street) {
                    $.toptip("地址不能为空");
                    $.hideLoading();
                    return;
                }
                lobsterh5.POST("/sso/v1/patient/EditAddress", address).then(res => {
                    $.hideLoading();
                    $.toast('保存成功', 'text');
                    setTimeout(() => {
                        location.href = "list.html";
                    }, 1500);
                }).catch(res => {
                    $.hideLoading();
                    $.toast(res.msg, 'forbidden');
                })
            });
            // $('.weui-switch').on('change', function () {
            //     var on = $(this).is(':checked');
            //     if (on) {
            //         $("[name='IsDefault']").val(1);
            //     } else {
            //         $("[name='IsDefault']").val(0);
            //     }
            // });
            $("#address").cityPicker({
                title: "选择地区",
                onChange: function (picker, values, displayValues) {
                    self.data.address.ProvinceCode = values[0];
                    self.data.address.CityCode = values[1];
                    self.data.address.DistrictCode = values[2];
                    self.data.address.Province = displayValues[0];
                    self.data.address.City = displayValues[1];
                    self.data.address.District = displayValues[2];
                    self.data.address.Address = displayValues[0] + " " + displayValues[1] + " " + displayValues[2];
                }
            });
        },
    })

</script>